package com.inspur.incubator.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.inspur.incubator.common.MD5Util;
import com.inspur.incubator.common.SerialNumberUtil;
import com.inspur.incubator.common.tools.EncoderMd5;
import com.inspur.incubator.data.Menu;
import com.inspur.incubator.data.MhUser;
import com.inspur.incubator.data.Role;
import com.inspur.incubator.data.Users;
import com.inspur.incubator.data.zTreeNode;
import com.inspur.incubator.mapper.RoleDaoMapper;
import com.inspur.incubator.service.RoleService;

@Service("roleService")
public class RoleServiceImpl implements RoleService{
	@Autowired
	RoleDaoMapper roleDaoMapper;
	EncoderMd5 md5 = new EncoderMd5();
	
	
	//获得选中账号的角色和所属单位
	public Map<String,Object> getCurrentInfo(String account){
	    return roleDaoMapper.getCurrentInfo(account);
	}
	
	//展示用户 ，通过角色等信息查询
	public List showUser(Map map) {
		return roleDaoMapper.show_user(map);
	}
	
	public List<String> getBankInfo(String index){
	    return roleDaoMapper.getBankInfo(index);
	}

	//添加账号 -- 先添加账户，然后再调用添加权限
	/**
	 * map id account password name roleid rolename
	 */
	public int addUser(MhUser mu) {
		
		mu.setUserPassword(MD5Util.ecodeByMD5(mu.getUserPassword()));//加密
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime=df.format(new Date());
		String userId=SerialNumberUtil.getSerialNumber();
		mu.setCreateTime(createTime);
		mu.setUserId(userId);
		mu.setUserType("0");//管理员
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("user_id",userId);
		map.put("account", mu.getUserName());
		map.put("role_id", mu.getRoleid());
		map.put("role_name", mu.getRolename());
			if(roleDaoMapper.insert_USER(mu)==1){
				//添加角色信息
				roleDaoMapper.insert_User_Role(map);
			}
			return 1;
		}

	//删除账号 
	public int deleteUser(Map map) {
		return 0;
	}

	//展示角色的权限
	@SuppressWarnings("unchecked")
	public Map show_Role_PERMISSION(String role_id) {
		List<Map> lists = roleDaoMapper.show_Role_PERMISSION(role_id);
		Map resultMap = new HashMap();
		List permissionList = new ArrayList();
		for(Map maps : lists) {
			permissionList.add(maps.get("PERMISSION"));
		}
		resultMap.put("role_id", role_id);
		resultMap.put("permission", permissionList);
		System.out.println("角色权限:"+resultMap.toString());
		return resultMap;
	}
	
	//更新角色功能权限-- 先delete再insert
	public int update_Role_PERMISSION(Map map) {
		int state1 = roleDaoMapper.delete_Role_PERMISSION(map);   //删除角色权限
		System.out.println("删除原有权限状态:"+state1);
		String permission = (String) map.get("permission");	
		String[] permissionTemp = permission.split("-");
		for (int i=0;i<permissionTemp.length;i++){
			System.out.println(permission);
			System.out.println(permissionTemp[i]);
			Map<String,String> map2 = new HashMap<String,String>();
			map2.put("permission", permissionTemp[i]);
			map2.put("role_id", (String)map.get("role_id"));
			map2.put("role_name", (String)map.get("role_name"));
			int state2 = roleDaoMapper.insert_Role_PERMISSION(map2);	  //添加角色权限
			System.out.println("权限添加状态:"+state2);
		}
		return state1;
	}

	@Override
	public int updateUser(Map map) {
		return 0;
	}

	public String updatePass(String account, String oldpass0,String newpass){
		String oldpass = roleDaoMapper.validateUser(account);
		try {
			oldpass0 =md5.EncoderByMd5(oldpass0);
			newpass = md5.EncoderByMd5(newpass);
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(!(oldpass.equals(oldpass0))){
			return "old";		//表明是原密码错误
		}
		Map map = new HashMap();
		map.put("password", newpass);
		map.put("account", account);
		int i= roleDaoMapper.updatePass(map);
		if(i==1){
			return "success";
		}else{
			return "error";
		}
	}

	@Override
	public List selectRole(String role_id) {
		// TODO Auto-generated method stub
		return roleDaoMapper.selectRole(role_id);
	}

	@Override
	public int getTabletotalnum(Map map) {
		return roleDaoMapper.getTabletotalnum(map);
	}

	@Override
	public List<Map> getTable(Map map) {
		return roleDaoMapper.getTable(map);
	}

	@Override
	public String resetPass(String newpass,String account) {
		Map map = new HashMap();
		map.put("password", newpass);
		map.put("account", account);
		int i= roleDaoMapper.updatePass(map);
		if(i==1){
			return "success";
		}else{
			return "error";
		}
	}

	public int cancelAccount(String account,String status) {
		Map map=new HashMap();
		map.put("account", account);
		map.put("status", status);
		return roleDaoMapper.cancelAccount(map);
	}

	public Users showAccount(String account) {
		// TODO Auto-generated method stub
		return roleDaoMapper.showAccount(account);
	}

	@Override
	public int updateAccount(Map map) {
		// TODO Auto-generated method stub
		//更新基本信息
		roleDaoMapper.updateAccount(map);
		//更新角色信息
		roleDaoMapper.updateRole(map);
		return 1;
	}

	@Override
	public List<Role> getRoles(Map map) {
		// TODO Auto-generated method stub
		return roleDaoMapper.queryRoles(map);
	}

	@Override
	public List<zTreeNode> getMenus(String role_id) {
		// TODO Auto-generated method stub
		List<Menu> menulist=roleDaoMapper.getMenus(role_id);
		List<zTreeNode> zlist=new ArrayList<zTreeNode>();
		
		for(Menu m:menulist){
			zTreeNode node=new zTreeNode();
			node.setId(m.getMENU_ID());
			node.setpId(m.getMENU_PID());
			node.setChecked(m.getCHECKED().equals("1"));
			node.setOpen(true);
			node.setName(m.getMENU_NAME());
			zlist.add(node);
		}
		return zlist;
	}

	@Override
	public void insertRoleMenu(List<zTreeNode> menus,String role_id) {
		// TODO Auto-generated method stub
		List<Menu> menulist=new ArrayList<Menu>();
		Map<String, Object> map=new HashMap();
		for(zTreeNode z:menus){
			Menu m=new Menu();
			if(z.getpId()!=null){
				m.setMENU_ID(z.getId());
				m.setROLE_ID(role_id);
				menulist.add(m);
			}
			
		}
		map.put("list", menulist);
		//删除role_menu
		roleDaoMapper.deleteRoleMenu(role_id);
		//新增role_menu
		roleDaoMapper.insertMenus(map);
	}

	@Override
	public List<Menu> getMemusInRole(String role_id) {
		// TODO Auto-generated method stub
		return roleDaoMapper.getMenuInRole(role_id);
	}

	@Override
	public int addRole(Map<String, String> map) {
		// TODO Auto-generated method stub
		return roleDaoMapper.insertRole(map);
	}
	
	
}
